<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends BaseController {

	public function __construct() {
        parent::__construct();
        // 如果是超管，就跳转到超管的控制器
        $webAdminUser = $this->session->webAdminUser ? $this->session->webAdminUser : NULL;
        if(empty($webAdminUser) || $webAdminUser['user_type'] != USER_TYPE_SUPER_ADMIN) show_error("请重新登录");
        // 加载库
        $this->load->model('webModel');
        // $this->load->model('schoolModel');
    }

    // 轮播图管理页面
    public function banner() {
        $this->menuLink = 'banner';
        $this->display('web' . DIRECTORY_SEPARATOR . "banner");
    }
    // 轮播图编辑页面
    public function banner_edit() {
        $this->menuLink = 'banner';
        $id = $this->get('id');
        if($id) {
            // 获取轮播图
            $banner = $this->webModel->get_banner_info($id);
            if($banner) {
                $this->banner = $banner;
            }
        }
        // 生成又拍云签名
        require_once('vendor/autoload.php');
        $serviceConfig = new Upyun\Config(UP_BUCKET, UP_USERNAME, UP_PASSWORD);
        $serviceConfig->setFormApiKey(UP_FORM_API);
        $serviceParams = $serviceConfig->getImageUploadParams(UP_FILEPATH, $this->policy, $this->signature);
        $this->display('web' . DIRECTORY_SEPARATOR . "banner_edit");
    }

    // 获取轮播图列表
    public function get_banner_list(){
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        $totalParams['isDelete'] = 0;
        $total = $this->webModel->get_banner_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['isDelete'] = 0;
        $bannerList = $this->webModel->get_banner_list($data);
        // 返回值
        exit(admin_table_data($total, $bannerList));
    }
    // 保存轮播图
    public function save_banner_action(){
        $bannerId = $this->post('id');
        $img = $this->post('img');
        $imgMob = $this->post('img-mob');
        $title = $this->post('title');
        $sort = $this->post("sort") ? $this->post("sort") : 0;
        // $link = $this->post("link");
        $link = '';  // 暂时不需要link

        // 内容检查
        if(empty($title)) throw new Exception("请填写标题");
        if(empty($img) && empty($imgMob)) throw new Exception("请上传图片");

        // 入库
        $data['img'] = $img;
        $data['img_mob'] = $imgMob;
        $data['title'] = $title;
        $data['link'] = $link;
        $data['sort'] = $sort;
        $data['is_delete'] = 0;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($bannerId) {
            $this->db->update(WebModel::$bannerTable, $data, "id='{$bannerId}'");
        } else {
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(WebModel::$bannerTable, $data);
        }
        // 返回值
        $msg = $bannerId ? '修改轮播图成功' : '添加轮播图成功';
        exit(json($msg, true));
    }
    // 删除轮播图
    public function delete_banner_action(){
        $bannerId = $this->post("id");
        // 执行删除操作
        $save['is_delete'] = 1;
        $this->db->update(WebModel::$bannerTable, $save, "id='{$bannerId}'");
        exit(json("删除成功", true));
    }
    // 产品分类管理页面
    public function product_category() {
        $this->menuLink = 'product_category';
        $this->display('web' . DIRECTORY_SEPARATOR . "product_category");
    }
    // 分类编辑页面
    public function product_category_edit() {
        $this->menuLink = 'product_category';
        $id = $this->get('id');
        if($id) {
            // 获取分类
            $this->productCategory = $this->webModel->get_product_category_info($id);
        }
        $this->display('web' . DIRECTORY_SEPARATOR . "product_category_edit");
    }
    // 获取分类列表
    public function get_product_category_list(){
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        $totalParams['isDelete'] = 0;
        $total = $this->webModel->get_product_category_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['isDelete'] = 0;
        $productCategoryList = $this->webModel->get_product_category_list($data);
        // 返回值
        exit(admin_table_data($total, $productCategoryList));
    }
    // 保存分类
    public function save_product_category_action(){
        $productCategoryId = $this->post('id');
        $title = $this->post('title');
        $sort = $this->post("sort") ? $this->post("sort") : 0;
        // 内容检查
        if(empty($title)) throw new Exception("请填写标题");
        // 入库
        $data['title'] = $title;
        $data['sort'] = $sort;
        $data['is_delete'] = 0;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($productCategoryId) {
            $this->db->update(WebModel::$productCategoryTable, $data, "id='{$productCategoryId}'");
        } else {
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(WebModel::$productCategoryTable, $data);
        }
        // 返回值
        $msg = $productCategoryId ? '修改分类成功' : '添加分类成功';
        exit(json($msg, true));
    }
    // 删除分类
    public function delete_product_category_action(){
        $productCategoryId = $this->post("id");
        // 执行删除操作
        $save['is_delete'] = 1;
        $this->db->update(WebModel::$productCategoryTable, $save, "id='{$productCategoryId}'");
        exit(json("删除成功", true));
    }
    // 产品管理页面
    public function product() {
        $this->menuLink = 'product';
        // 获取产品分类列表
        $this->categoryList = $this->webModel->get_product_category_list();
        $this->display('web' . DIRECTORY_SEPARATOR . "product");
    }
    // 产品编辑页面
    public function product_edit() {
        $this->menuLink = 'product';
        $id = $this->get('id');
        if($id) {
            // 获取产品
            $product = $this->webModel->get_product_info($id);
            if($product) {
	            // // 替换<br>为\n
                // $product['content'] = transformBr($product['content']);
                $this->product = $product;
            }
        }
        // 获取产品分类列表
        $this->categoryList = $this->webModel->get_product_category_list();
        // 生成又拍云签名
        require_once('vendor/autoload.php');
        $serviceConfig = new Upyun\Config(UP_BUCKET, UP_USERNAME, UP_PASSWORD);
        $serviceConfig->setFormApiKey(UP_FORM_API);
        $serviceParams = $serviceConfig->getImageUploadParams(UP_FILEPATH, $this->policy, $this->signature);
        $this->display('web' . DIRECTORY_SEPARATOR . "product_edit");
    }
    // 获取产品列表
    public function get_product_list(){
        if($this->get('cate')) {
            $totalParams['cateId'] = $data['cateId'] = $this->get('cate');
        }
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        $totalParams['isDelete'] = 0;
        $total = $this->webModel->get_product_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['isDelete'] = 0;
        $productList = $this->webModel->get_product_list($data);
        // 获取产品分类列表
        $categoryList = $this->webModel->get_product_category_list();
        // 使用id作为键值
        $categoryList = array_value_to_key($categoryList, 'id');
        foreach($productList as &$product) {
            $product['cateTitle'] = $categoryList[$product['cate_id']]['title'];
        }
        // 返回值
        exit(admin_table_data($total, $productList));
    }
    // 保存产品
    public function save_product_action(){
        $productId = $this->post('id');
        $img = $this->post('img');
        $title = $this->post('title');
        $content = $this->post('content', false);
        $contentDetail = $this->post('content_detail');
        $sort = $this->post("sort") ? $this->post("sort") : 0;
        $cate = $this->post("cate");
        // 替换换行符为<br>，空格符为&nbsp;
        // $content = transformToBr($content);
        // 内容检查
        if(empty($title)) throw new Exception("请填写标题");
        if(empty($cate)) throw new Exception('请选择分类');

        // 入库
        $data['img'] = $img;
        $data['title'] = $title;
        $data['content'] = $content;
        $data['content_detail'] = $contentDetail;
        $data['sort'] = $sort;
        $data['cate_id'] = $cate;
        $data['is_delete'] = 0;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($productId) {
            $this->db->update(WebModel::$productTable, $data, "id='{$productId}'");
        } else {
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(WebModel::$productTable, $data);
        }
        // 返回值
        $msg = $productId ? '修改产品成功' : '添加产品成功';
        exit(json($msg, true));
    }
    // 删除产品
    public function delete_product_action(){
        $productId = $this->post("id");
        // 执行删除操作
        $save['is_delete'] = 1;
        $this->db->update(WebModel::$productTable, $save, "id='{$productId}'");
        exit(json("删除成功", true));
    }

    // 新闻管理页面
    public function news() {
        $this->menuLink = 'news';
        $this->display('web' . DIRECTORY_SEPARATOR . "news");
    }
    // 新闻编辑页面
    public function news_edit() {
        $this->menuLink = 'news';
        $id = $this->get('id');
        if($id) {
            // 获取新闻详情
            $news = $this->webModel->get_news_info($id);
            if($news) {
	            // // 替换<br>为\n
                // $news['content'] = transformBr($news['content']);
                $this->news = $news;
            }
        }
        $this->display('web' . DIRECTORY_SEPARATOR . "news_edit");
    }

    // 获取新闻列表
    public function get_news_list(){
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        $totalParams['isDelete'] = 0;
        $total = $this->webModel->get_news_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['isDelete'] = 0;
        $newsList = $this->webModel->get_news_list($data);
        // 返回值
        exit(admin_table_data($total, $newsList));
    }
    // 保存新闻
    public function save_news_action(){
        $newsId = $this->post('id');
        $title = $this->post('title');
        $content = $this->post('content', false);
        // 替换换行符为<br>，空格符为&nbsp;
        // $content = transformToBr($content);
        // 内容检查
        if(empty($title)) throw new Exception("请填写标题");
        if(empty($content)) throw new Exception("请填写内容");

        // 入库
        $data['title'] = $title;
        $data['content'] = $content;
        $data['is_delete'] = 0;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($newsId) {
            $this->db->update(WebModel::$newsTable, $data, "id='{$newsId}'");
        } else {
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(WebModel::$newsTable, $data);
        }
        // 返回值
        $msg = $newsId ? '修改新闻成功' : '添加新闻成功';
        exit(json($msg, true));
    }
    // 删除轮播图
    public function delete_news_action(){
        $newsId = $this->post("id");
        // 执行删除操作
        $save['is_delete'] = 1;
        $this->db->update(WebModel::$newsTable, $save, "id='{$newsId}'");
        exit(json("删除成功", true));
    }

    // 案例管理页面
    public function cases() {
        $this->menuLink = 'cases';

        $this->display('web' . DIRECTORY_SEPARATOR . "cases");
    }
    // 案例编辑页面
    public function case_edit() {
        $this->menuLink = 'cases';
        $id = $this->get('id');
        if($id) {
            // 获取案例
            $case = $this->webModel->get_case_info($id);
            if($case) {
                // 替换<br>为\n
                // $case['content'] = transformBr($case['content']);
                $this->case = $case;
            }
        }
        // 生成又拍云签名
        require_once('vendor/autoload.php');
        $serviceConfig = new Upyun\Config(UP_BUCKET, UP_USERNAME, UP_PASSWORD);
        $serviceConfig->setFormApiKey(UP_FORM_API);
        $serviceParams = $serviceConfig->getImageUploadParams(UP_FILEPATH, $this->policy, $this->signature);
        $this->display('web' . DIRECTORY_SEPARATOR . "case_edit");
    }

    // 获取案例列表
    public function get_case_list(){
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        $totalParams['isDelete'] = 0;
        $total = $this->webModel->get_case_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['isDelete'] = 0;
        $caseList = $this->webModel->get_case_list($data);
        // 返回值
        exit(admin_table_data($total, $caseList));
    }
    // 保存案例
    public function save_case_action(){
        $caseId = $this->post('id');
        $img = $this->post('img');
        $title = $this->post('title');
        $content = $this->post('content', false);
        $sort = $this->post("sort") ? $this->post("sort") : 0;
        // 替换换行符为<br>，空格符为&nbsp;
        // $content = transformToBr($content);
        // 内容检查
        if(empty($title)) throw new Exception("请填写标题");
        if(empty($img)) throw new Exception("请上传图片");

        // 入库
        $data['img'] = $img;
        $data['title'] = $title;
        $data['content'] = $content;
        $data['sort'] = $sort;
        $data['is_delete'] = 0;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($caseId) {
            $this->db->update(WebModel::$caseTable, $data, "id='{$caseId}'");
        } else {
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(WebModel::$caseTable, $data);
        }
        // 返回值
        $msg = $caseId ? '修改案例成功' : '添加案例成功';
        exit(json($msg, true));
    }
    // 删除案例
    public function delete_case_action(){
        $caseId = $this->post("id");
        // 执行删除操作
        $save['is_delete'] = 1;
        $this->db->update(WebModel::$caseTable, $save, "id='{$caseId}'");
        exit(json("删除成功", true));
    }
}